package com.changgou.goods.controller;

import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.Id;
import java.util.List;

/**
 * @Author: wzw
 * @Date: 2020/12/6 22:17
 * @version: 1.8
 */
@RestController
@RequestMapping("/brand")
public class BrandController {

    //注入service层
    @Autowired
    private BrandService brandService;

    /**
     * 查询:查询所有品牌
     * @return 所有品牌
     */
    @GetMapping
    public Result<List<Brand>> findAll(){
        try {
            //休眠,用于测试并发连接
            Thread.sleep(15000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("休眠结束,测试并发数量:"+Thread.currentThread());

        //实现功能:查询所有
        List<Brand> list = brandService.findAll();
        //返回值(判断,状态码,提示,结果)
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 查询: 根据ID查询品牌
     */
    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable(value = "id") Integer id){
        //实现功能:查询一个
        Brand brand = brandService.findById(id);
        //返回值
        return new Result<>(true, StatusCode.OK, "查询成功", brand);
    }

    /**
     * 添加:新增品牌
     */
    @PostMapping
    public Result add(@RequestBody Brand brand){
        int add = brandService.add(brand);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /**
     * 修改:修改品牌
     */
    @PutMapping("/{id}")
    public Result update(@RequestBody Brand brand,@PathVariable(value = "id") Integer id){
        //设置要修改的主键
        brand.setId(id);
        //实现功能
        int update = brandService.update(brand);
        //处理结果
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /***
     * 删除品牌
     * @param id
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable(value = "id") Integer id){
        //实现功能:删除品牌
        int i =brandService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");

    }

    /**
     * 品牌列表条件查询
     */
    @PostMapping("/search")
    public Result<List<Brand>> findList(@RequestBody(required = false) Brand brand){
        //事项功能
        List<Brand> list = brandService.findList(brand);
        //返回结果
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/search/{page}/{size}")
    public Result<PageInfo<Brand>> search(@PathVariable(value = "page")Integer page,@PathVariable(value = "size")Integer size){
        //分页查询
        PageInfo<Brand> pageInfo = brandService.findPage(page,size);
        //返回结果
        return new Result<PageInfo<Brand>>(true, StatusCode.OK, "查询成功",pageInfo);
    }

    /**
     * 分页+条件
     */
    @PostMapping("/search/{page}/{size}")
    public Result<PageInfo> findPage(@RequestBody(required = false)Brand brand,@PathVariable(value = "page")Integer page,@PathVariable(value = "size")Integer size){
        //实现功能
        PageInfo<Brand> pageInfo = brandService.findPage(brand, page, size);

        //返回结果
        return new Result<>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /**
     * @author wzw
     * 根据分类实现品牌列表查询
     * @Date 21:34 2020/12/10
     * @param categoryId 分类ID
     * @return entity.Result
    **/
    @GetMapping("/category/{id}")
    public Result findBrandByCategory(@PathVariable(value = "id")Integer categoryId){
        //实现功能
        List<Brand> categoryList = brandService.findByCategory(categoryId);

        //返回结果集
        return new Result(true, StatusCode.OK, " 查询分类对应的品牌集合成功", categoryList);

    }

}
